package com.detech.sbom.base.config.swagger;

import com.detech.sbom.base.annotation.DevProfile;
import com.detech.sbom.base.enumtype.CodeEnum;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@EnableOpenApi  //swagger 3
@EnableKnife4j  //Knife4j
@Configuration
@DevProfile //此配置仅在dev环境下生效
public class SwaggerConfigDev {
    @Bean
    public Docket createRestApiForAll() {
        //添加全局响应状态码
        List<Response> responseList = new ArrayList<>();
        Arrays.stream(CodeEnum.values()).forEach(codeEnum -> {
            responseList.add(
                    new ResponseBuilder().code(Integer.toString(codeEnum.getCode())).description(codeEnum.getMsg()).build()
            );
        });
        return new Docket(DocumentationType.OAS_30)
                .useDefaultResponseMessages(false)
                .globalResponses(HttpMethod.GET, responseList)
                .globalResponses(HttpMethod.POST, responseList)
                .apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.detech.sbom"))
                .paths(PathSelectors.any()).build()
                .groupName("所有API").pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot样例项目")
                .termsOfServiceUrl("")
                .contact(new Contact("方安伦","https://gitee.com/allen356/","byzxedu@163.com"))
                .description("这是一个示例项目，Swagger3.0 + Knife4j3.0.3 ")
                .version("1.0")
                .build();
    }

    /***********************************根据包路径进行分组************************************/

    //登录模块
    @Bean
    public Docket createRestApiForLogin() {
        //添加全局响应状态码
        List<Response> responseList = new ArrayList<>();
        Arrays.stream(CodeEnum.values()).forEach(codeEnum -> {
            responseList.add(
                    new ResponseBuilder().code(Integer.toString(codeEnum.getCode())).description(codeEnum.getMsg()).build()
            );
        });
        return new Docket(DocumentationType.OAS_30)
                .useDefaultResponseMessages(false)
                .globalResponses(HttpMethod.GET, responseList)
                .globalResponses(HttpMethod.POST, responseList)
                .apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.detech.sbom.business.login.controller"))
                .paths(PathSelectors.any()).build().groupName("登录").pathMapping("/");
    }

    //日志管理
    @Bean
    public Docket createRestApiForLog() {
        //添加全局响应状态码
        List<Response> responseList = new ArrayList<>();
        Arrays.stream(CodeEnum.values()).forEach(codeEnum -> {
            responseList.add(
                    new ResponseBuilder().code(Integer.toString(codeEnum.getCode())).description(codeEnum.getMsg()).build()
            );
        });
        return new Docket(DocumentationType.OAS_30)
                .useDefaultResponseMessages(false)
                .globalResponses(HttpMethod.GET, responseList)
                .globalResponses(HttpMethod.POST, responseList)
                .apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.detech.sbom.base.log.controller"))
                .paths(PathSelectors.any()).build().groupName("日志管理").pathMapping("/");
    }

    //用户管理
    @Bean
    public Docket createRestApiForUser() {
        //添加全局响应状态码
        List<Response> responseList = new ArrayList<>();
        Arrays.stream(CodeEnum.values()).forEach(codeEnum -> {
            responseList.add(
                    new ResponseBuilder().code(Integer.toString(codeEnum.getCode())).description(codeEnum.getMsg()).build()
            );
        });
        return new Docket(DocumentationType.OAS_30)
                .useDefaultResponseMessages(false)
                .globalResponses(HttpMethod.GET, responseList)
                .globalResponses(HttpMethod.POST, responseList)
                .apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.detech.sbom.business.user.controller"))
                .paths(PathSelectors.any()).build().groupName("用户管理").pathMapping("/");
    }

}
